package com.service.impl;

import com.dao.ExamrecordDao;
import com.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    private ExamrecordDao examrecordMapper;

    @Override
    public List<Map<String, Object>> getStudentScoreDistribution() {
        // 从数据库查询原始数据
        List<Map<String, Object>> rawData = examrecordMapper.getScoreDistribution();

        // 转换为前端需要的格式
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> item : rawData) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", item.get("level"));
            data.put("value", item.get("count"));
            result.add(data);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getStudentAvgScores() {
        // 从数据库查询原始数据
        List<Map<String, Object>> rawData = examrecordMapper.selectStudentAvgScores();

        // 转换为前端需要的格式
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> item : rawData) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", item.get("name"));
            data.put("avgScore", item.get("avg_score"));
            result.add(data);
        }
        return result;
    }
}
